import { Local } from '@/utils/storage'
import { usePublic } from '@/utils/public'
const { toLogin } = usePublic()



export const authRequest = <T>(options: RequestOption, showLoading = true) => {
    return new Promise<Result<T>>((resolve, reject) => {
        options.url = import.meta.env.VITE_API_BASE_URL + options.url
        const token = Local.get('token') ? Local.get('token') : ''
        options.header = {
            'content-type': 'application/json',
            Authorization: token ? ` ${token}` : '',
            ...options.header, 
        }
        if (showLoading) {
            uni.showLoading({
                title: '玩命加载中...',
                mask: true
            })
        }
        uni.request({
            ...options,
            success(res: any) {
                if (res.statusCode == 401) {
                    toLogin()
                    return
                } else if (res.statusCode !== 200) {
                    setTimeout(() => {
                        uni.showToast({
                            title: res.data.desc,
                            icon: 'none',
                            duration: 3000
                        })
                    }, 1000)
                    console.log('options', options)
                    console.log('res', res)
                    reject(`服务器异常${options.url}`)
                }
                const data = res.data as Result<T>
                resolve(data as Result<T>)
            },
            complete() {
                uni.stopPullDownRefresh()
                uni.hideLoading()
            }
        })
    })
}
//mock请求
export const authRequest2 = <T>(options: RequestOption, showLoading = true) => {
    return new Promise<Result<T>>((resolve, reject) => {
        options.url = import.meta.env.VITE_API_BASE_URL2 + options.url
        const token = Local.get('access_token') ? Local.get('access_token') : ''
        options.header = {
            'content-type': 'application/json',
            Authorization: token ? `Bearer ${token}` : '',
            ...options.header
        }
        if (showLoading) {
            uni.showLoading({
                title: '玩命加载中...',
                mask: true
            })
        }
        uni.request({
            ...options,
            success(res: any) {
                if (res.statusCode == 401) {
                    toLogin()
                    return
                } else if (res.statusCode !== 200) {
                    setTimeout(() => {
                        uni.showToast({
                            title: res.data.desc,
                            icon: 'none',
                            duration: 3000
                        })
                    }, 1000)
                    console.log('options', options)
                    console.log('res', res)
                    reject(`服务器异常${options.url}`)
                }
                const data = res.data as Result<T>
                resolve(data as Result<T>)
            },
            complete() {
                uni.stopPullDownRefresh()
                uni.hideLoading()
            }
        })
    })
}
